package com.simple.common;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.simple.common.enumdto.HttpStatus;
import lombok.Data;

/**
 * <p>
 *     <br/>客户端相应结果
 * </p>
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
@Data
public class Result {

    /**
     * @JsonProperty作用：把该属性的名称序列化为另外一个名称，如把trueName属性序列化为name
     */
    @JsonProperty
    private Integer code;
    private String message;
    private Object data;

    public Result(Object data) {
        this.data = data;
    }

    public Result(Integer code, String message, Object data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static Result of(HttpStatus httpStatus, Object data) {
        return new Result(httpStatus.getCode(),httpStatus.getMsg(),data);
    }

    public static Result of(int code, String message) {
        return new Result(code, message,null);
    }
    public static Result of(int code, String message,Object data) {
        return new Result(code, message,data);
    }

    public static Result of() {
        return of(HttpStatus.SUCCESS);
    }
    public static Result of(HttpStatus httpStatus) {
        return new Result(httpStatus.getCode(),httpStatus.getMsg(),null);
    }
    public static Result of(Object data) {
        return of(HttpStatus.SUCCESS,data);
    }

    /**
     * 操作失败
     * @return
     */
    public static Result error() {
        return of(HttpStatus.FAIL);
    }

    /**
     * 参数异常
     * @param httpStatus
     * @param o
     * @return
     */
    public static Result error(HttpStatus httpStatus,Object o) {
        return of(httpStatus,o);
    }

    public static Result error(int code,String msg) {
        return of(code, msg);
    }

    /**
     * 未知异常
     * @param o
     * @return
     */
    public static Result error(Object o) {
        return of(HttpStatus.ERROR,o);
    }

    @Override
    public String toString() {
        return "Result{" +
                "code=" + code +
                ", message='" + message + '\'' +
                ", data=" + data +
                '}';
    }
}
